# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=testtools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.3.0
pkgrel=1
pkgdesc="Extensions to the Python standard library unit testing framework (mingw-w64)"
arch=('any')
url='https://github.com/testing-cabal/testtools'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-pbr"
         "${MINGW_PACKAGE_PREFIX}-python-extras"
         "${MINGW_PACKAGE_PREFIX}-python-fixtures"
         "${MINGW_PACKAGE_PREFIX}-python-pyrsistent"
         "${MINGW_PACKAGE_PREFIX}-python-mimeparse")
makedepends=('git' 
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/testing-cabal/testtools/archive/${pkgver}.tar.gz"
        63d6945020f81d8e85aad7bafdb6bdd087d3ffbc.patch
        33dc6c8c67620863d14622df49e9834ac6fe72b7.patch
        0a609e10198a818ba21d607107a667cb95afef0a.patch
        e83040c34dfbd908a784fe3bde9f948eee82cfc1.patch
        f41ee7224121a9b74dab1c91b3fede44c9c34029.patch
        fff42432c60784be1a974ffd69afb35f093c334a.patch
        29004731f9c480b7c44a9c2605513d50d372898f.patch
        f865cfefae73e540aedb16ebb19f813cc648f2b6.patch)
sha512sums=('7c3b9e180c5f22d613763446525fcb3f7f260d79bc875669134c3077af0e0d3e101863429e0469ffcb26fd0755b2c915454abc949be864639a2faa9cf5024c62'
            '589be8461f71128eeb388a6c5813b1315a363f8b38c07d7437254fcddf12562a4a17a5b809481304e82e219848afe80c09d5fb3b5c46f40d2b66790607a7a674'
            'ed961b7eefbf62755fb59c63413826cf0779bd664e5b7dd19d9319690eb3dafcec79cdad4d5d929c59bc8fc8381b64d7d882a46c872ec1da3dca7de6eaf23a0d'
            '41f277d062146f8a85822ede38ca8e0dd661f10b5ed5ac47a2555be2b014415fba53917edffc1ee10d178d5098e713f98ce62250e9f9cc64df826f05839b8371'
            'd58129a90857202b1d7cf03cb323a3730f083671403fc4b15c5e0b2fb605b94bae0b20d189511c16bab323cd2a5ae12a0274fffe18b723111f530f18c5622063'
            'b49530ec88f5ea5ccee99dcb1e52a0dc3d97f19c8f59dac93ec1c5c8ea0d22041bcbd2d928a5198153f55e74c220dd67027b0cfcdfe9a49eda2e6ed227132993'
            '11f3b6c3ef251ab4dae7e6345dc17728e153733dbab4dbdc6880670c4123b39b6e76ca7def8c984e6aa66a7e88bf5d86e1de593dac43bc35d6cd55414792fc05'
            '66572c0578aa1a393ee0502b83fe3bdc38f4de039c9eea8e0aad8d19deb946c965bdd3d30d4ee2120699fadd8ea5f81a12e939a854d8b771856b871c1743a009'
            '66572c0578aa1a393ee0502b83fe3bdc38f4de039c9eea8e0aad8d19deb946c965bdd3d30d4ee2120699fadd8ea5f81a12e939a854d8b771856b871c1743a009')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
  pushd "${_realname}-${pkgver}" 
  apply_patch_with_msg \
    63d6945020f81d8e85aad7bafdb6bdd087d3ffbc.patch \
    33dc6c8c67620863d14622df49e9834ac6fe72b7.patch \
    0a609e10198a818ba21d607107a667cb95afef0a.patch \
    e83040c34dfbd908a784fe3bde9f948eee82cfc1.patch \
    f41ee7224121a9b74dab1c91b3fede44c9c34029.patch \
    fff42432c60784be1a974ffd69afb35f093c334a.patch 
#    29004731f9c480b7c44a9c2605513d50d372898f.patch \
  apply_patch_with_msg \
    f865cfefae73e540aedb16ebb19f813cc648f2b6.patch
  popd

  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"

  # These are for python 2.x and old python 3.x only
  sed -i 's|unittest2|unittest|' python-build-${CARCH}/testtools/tests/test_run.py \
                                 python-build-${CARCH}/testtools/tests/test_testsuite.py \
                                 python-build-${CARCH}/testtools/tests/twistedsupport/test_matchers.py \
                                 python-build-${CARCH}/testtools/tests/twistedsupport/test_deferred.py \
                                 python-build-${CARCH}/testtools/tests/twistedsupport/test_runtest.py
  sed -i '/unittest2>=1.0.0/d' python-build-${CARCH}/requirements.txt
  sed -i 's|import linecache2 as linecache|import linecache|' python-build-${CARCH}/testtools/tests/test_compat.py
  sed -i "s|traceback = try_import('traceback2')|import traceback|" python-build-${CARCH}/testtools/content.py
  sed -i '/traceback2/d' python-build-${CARCH}/requirements.txt

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
  export PBR_VERSION=${pkgver}
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python -m testtools.run testtools.tests.test_suite
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
